﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.AspNet.Membership.OpenAuth;
using System.Data.SqlClient;
using System.Configuration;


public partial class Account_Register : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        RegisterUser.EnableViewState = true;
        
        RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];
        
        if (!Page.IsPostBack)
        {
            RolesDataBind();
        }
        
          
    }
    protected void RolesDataBind()
    {

        ((DropDownList)(RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("MyRole"))).DataSource = Roles.GetAllRoles();
        ((DropDownList)(RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("MyRole"))).DataBind();
    }


    
    
    
    
    
    protected void RegisterUser_CreatedUser(object sender, EventArgs e)
    {
        FormsAuthentication.SetAuthCookie(RegisterUser.UserName, createPersistentCookie: false);

        
        //Add the user profile information into the table UserProfile
        // Get the UserId of the just-added user 
        MembershipUser newUser = Membership.GetUser(RegisterUser.UserName);

        Guid newUserId = (Guid)newUser.ProviderUserKey;


        //Add role to user
        string RoleName;
        RoleName = ((DropDownList)(RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("MyRole"))).SelectedValue;
        Roles.AddUserToRole(newUser.UserName, RoleName);

        //Get Profile Data Entered by user in CUW control 
        String FirstName = ((TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("FirstName")).Text;
        String LastName = ((TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("LastName")).Text;
        String CellPhone = ((TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("CellPhone")).Text;
        // Insert a new record into User_Profile

        // Get your Connection String from the web.config. 

        string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        string insertSql = "INSERT INTO UserProfile(UserId,FirstName,LastName,CellPhone) VALUES(@UserId, @FirstName,@LastName,@CellPhone)";

        using (SqlConnection myConnection = new SqlConnection(connectionString))
        {

            myConnection.Open();
            SqlCommand myCommand = new SqlCommand(insertSql, myConnection);
            myCommand.Parameters.AddWithValue("@UserId", newUserId);
            myCommand.Parameters.AddWithValue("@FirstName", FirstName);
            myCommand.Parameters.AddWithValue("@LastName", LastName);
            myCommand.Parameters.AddWithValue("@CellPhone", CellPhone);
            myCommand.ExecuteNonQuery();

            myConnection.Close();

        }


        string continueUrl = RegisterUser.ContinueDestinationPageUrl;
        if (!OpenAuth.IsLocalUrl(continueUrl))
        {
            continueUrl = "~/";
        }
        Response.Redirect(continueUrl);
    }
 
}